package com.xpgk.mvc.organizationLevel;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.jfinal.log.Log;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.xpgk.annotation.Service;
import com.xpgk.constant.ConstantRender;
import com.xpgk.dto.RenderBean;
import com.xpgk.dto.ZtreeNode;
import com.xpgk.mvc.base.BaseService;
import com.xpgk.mvc.deptStation.DeptStation;
import com.xpgk.mvc.deptStation.DeptStationRight;
import com.xpgk.mvc.deptStation.DeptStationService;
import com.xpgk.mvc.organization.Organization;
import com.xpgk.mvc.right.Right;

@Service(name = OrganLevelService.serviceName)
public class OrganLevelService extends BaseService {

	@SuppressWarnings("unused")
	private static final Log log = Log.getLog(OrganLevelService.class);

	public static final String serviceName = "organLevelService";

	/**
	 * 获取机构子节点数据
	 * 
	 * @param cxt
	 * @param parentIds
	 * @return
	 */
	public List<ZtreeNode> childNodeData(String cxt, String ids, String curOrganIds) {
		List<OrganLevel> list = null;
		if (null != ids) {
			String sql = getSqlMy(OrganLevel.sqlId_childNode);
			list = OrganLevel.dao.find(sql, ids);

		} else {
			list = new ArrayList<>();
			list.add(OrganLevel.dao.findById(Organization.dao.findById(curOrganIds).getStr(Organization.column_levelIds)));
		}

		List<ZtreeNode> nodeList = new ArrayList<ZtreeNode>();
		ZtreeNode node = null;

		for (OrganLevel right : list) {
			node = new ZtreeNode();
			node.setId(right.getPKValue());
			node.setName(right.getStr(OrganLevel.column_name));
			node.setIsParent(right.getStr(OrganLevel.column_isparent));
			node.setIsHidden(right.getStr(OrganLevel.column_isHidden));
			node.setIcon(cxt + ConstantRender.icon_path + right.getStr(OrganLevel.column_icon));
			nodeList.add(node);
		}

		return nodeList;
	}

	/**
	 * 获取right子节点数据
	 * 
	 * @param cxt
	 * @param parentIds
	 * @return
	 */
	public List<ZtreeNode> rightChildNodeDataSystem(String cxt, Map<String, Object> queryParam, String levelIds) {
		List<Record> list = null;
		if (null != queryParam.get(Right.column_ids)) {
			queryParam.put(Right.column_parentids, queryParam.get(Right.column_ids));
			list = find(OrganLevelRight.sqlId_rightChildNodeSystem, queryParam);

		} else {
			String sql = getSqlMy(OrganLevelRight.sqlId_rightRootNodeSystem);
			list = Db.find(sql, levelIds);
		}

		List<ZtreeNode> nodeList = new ArrayList<ZtreeNode>();
		ZtreeNode node = null;

		for (Record right : list) {
			node = new ZtreeNode();
			node.setId(right.getStr(Right.column_ids));
			node.setName(right.getStr(Right.column_name));
			node.setIsParent(right.getStr(Right.column_isparent));
			node.setChecked(right.getStr("checked"));
			node.setIcon(cxt + ConstantRender.icon_path + right.getStr(Right.column_icon));
			nodeList.add(node);
		}

		return nodeList;
	}
	
	/**
	 * 获取right子节点数据
	 * 
	 * @param cxt
	 * @param parentIds
	 * @return
	 */
	public List<ZtreeNode> rightChildNodeData(String cxt, String levelIds, String curLevelIds, String ids) {
		List<Record> list = null;
		if (null != ids) {
			String sql = getSqlMy(OrganLevelRight.sqlId_rightChildNode);
			list = Db.find(sql, levelIds, curLevelIds, ids);

		} else {
			String sql = getSqlMy(OrganLevelRight.sqlId_rightRootNode);
			list = Db.find(sql, levelIds, curLevelIds);
		}

		List<ZtreeNode> nodeList = new ArrayList<ZtreeNode>();
		ZtreeNode node = null;

		for (Record right : list) {
			node = new ZtreeNode();
			node.setId(right.getStr("rightIds"));
			node.setName(right.getStr("name"));
			node.setIsParent(right.getStr("isparent"));
			node.setChecked(right.getStr("checked"));
			node.setIcon(cxt + ConstantRender.icon_path + right.getStr(Right.column_icon));
			nodeList.add(node);
		}

		return nodeList;
	}

	/**
	 * 添加权限
	 * 
	 * @param ids
	 * @param levelIds
	 * @param result
	 * @return
	 */
	public RenderBean addLevelRight(String ids, String levelIds, RenderBean result, String curLevelIds) {
		// 添加自己
		Right right = Right.dao.findById(ids);
		OrganLevelRight olr = OrganLevelRight.dao.findByIds(ids, levelIds);
		if(null == olr){
			new OrganLevelRight().set(OrganLevelRight.column_levelIds, levelIds).set(OrganLevelRight.column_rightIds, ids).save();
		}
		List<DeptStation> stations = DeptStation.dao.find(getSqlMy(DeptStation.sqlId_findRootStationByLevelIds), levelIds);
		if(stations.size() > 0){//批量添加根岗位
			for(DeptStation station : stations){
				DeptStationRight oldStation = DeptStationRight.dao.findByIds(ids, station.getPKValue());
				if(null == oldStation){
					new DeptStationRight()
					.set(DeptStationRight.column_rightIds, ids)
					.set(DeptStationRight.column_stationIds, station.getPKValue()).save();
				}
			}
		}

		// 添加子节点
		addChildLevelRight(ids, levelIds, stations, curLevelIds);

		// 添加父节点
		addParentLevelRight(right.getStr(Right.column_parentids), levelIds, stations);
		
		//跟新岗位缓存
		if(stations.size() > 0){
			for(DeptStation station : stations){
				DeptStationService.cacheAdd(station.getPKValue());
			}
		}

		result.setCode(ConstantRender.render_success_code);
		return result;
	}

	// 递归添加子节点
	public void addChildLevelRight(String ids, String levelIds, List<DeptStation> stations, String curLevelIds) {
		String sql =null;
		List<Record> childs = new ArrayList<>();
		if(curLevelIds.equals(OrganLevel.system)){
			Map<String, Object> queryParam = new HashMap<String, Object>();
			queryParam.put(Right.column_parentids, ids);
			queryParam.put(Right.column_privilegess, OrganLevelController.privilegess);
			queryParam.put(Right.column_status, OrganLevelController.status);
			childs = find(OrganLevelRight.sqlId_rightChildNodeSystem, queryParam);
		}else{
			sql = getSqlMy(OrganLevelRight.sqlId_rightChildNode);
			childs = Db.find(sql, levelIds, curLevelIds, ids);
		}
		if (childs.size() > 0) {
			for (Record child : childs) {
				OrganLevelRight olr = OrganLevelRight.dao.findByIds(child.getStr("rightIds"), levelIds);
				if (null == olr) {
					olr = new OrganLevelRight();
					olr.set(OrganLevelRight.column_levelIds, levelIds)
							.set(OrganLevelRight.column_rightIds, child.getStr("rightIds")).save();
				}
				if(stations.size() > 0){//批量添加根岗位
					for(DeptStation station : stations){
						DeptStationRight oldStation = DeptStationRight.dao.findByIds(child.getStr("rightIds"), station.getPKValue());
						if(null == oldStation){
							new DeptStationRight()
							.set(DeptStationRight.column_rightIds, child.getStr("rightIds"))
							.set(DeptStationRight.column_stationIds, station.getPKValue()).save();
						}
					}
				}
				addChildLevelRight(child.getStr("rightIds"), levelIds, stations, curLevelIds);
			}
		}

	}

	// 递归添加父节点
	public void addParentLevelRight(String ids, String levelIds, List<DeptStation> stations) {
		if (null != ids) {
			Right right = Right.dao.findById(ids);
			if (null != right) {
				if(stations.size() > 0){//批量添加根岗位
					for(DeptStation station : stations){
						DeptStationRight oldStation = DeptStationRight.dao.findByIds(ids, station.getPKValue());
						if(null == oldStation){
							new DeptStationRight()
							.set(DeptStationRight.column_rightIds, ids)
							.set(DeptStationRight.column_stationIds, station.getPKValue()).save();
						}
					}
				}
				OrganLevelRight olr = OrganLevelRight.dao.findByIds(ids, levelIds);
				if (null == olr) {
					olr = new OrganLevelRight();
					olr.set(OrganLevelRight.column_levelIds, levelIds)
							.set(OrganLevelRight.column_rightIds, right.getPKValue()).save();
					addParentLevelRight(right.getStr(Right.column_parentids), levelIds, stations);
				}
			}
		}
	}

	/**
	 * 删除权限
	 * 
	 * @param ids
	 * @param levelIds
	 * @param result
	 * @return
	 */
	public RenderBean delLevelRight(String ids, String levelIds, RenderBean result, String curLevelIds) {
		// 删除自己
		Right right = Right.dao.findById(ids);
		OrganLevelRight olr = OrganLevelRight.dao.findByIds(ids, levelIds);
		if(null != olr){
			olr.delete();
		}
		List<DeptStation> stations = DeptStation.dao.find(getSqlMy(DeptStation.sqlId_findStationByLevelIds), levelIds);
		if(stations.size() > 0){//批量删除岗位
			for(DeptStation station : stations){
				DeptStationRight oldStation = DeptStationRight.dao.findByIds(ids, station.getPKValue());
				if(null != oldStation){
					oldStation.delete();
				}
			}
		}

		// 删除子节点
		delChildLevelRight(ids, levelIds, stations, curLevelIds);

		// 删除父节点
		delParentLevelRight(right.getStr(Right.column_parentids), levelIds, stations);
		
		//跟新岗位缓存
		if(stations.size() > 0){
			for(DeptStation station : stations){
				DeptStationService.cacheAdd(station.getPKValue());
			}
		}

		result.setCode(ConstantRender.render_success_code);
		return result;
	}

	// 递归删除子节点
	public void delChildLevelRight(String ids, String levelIds, List<DeptStation> stations, String curLevelIds) {
		String sql =null;
		List<Record> childs = new ArrayList<>();
		if(curLevelIds.equals(OrganLevel.system)){
			Map<String, Object> queryParam = new HashMap<String, Object>();
			queryParam.put(Right.column_parentids, ids);
			queryParam.put(Right.column_privilegess, OrganLevelController.privilegess);
			queryParam.put(Right.column_status, OrganLevelController.status);
			childs = find(OrganLevelRight.sqlId_rightChildNodeSystem, queryParam);
		}else{
			sql = getSqlMy(OrganLevelRight.sqlId_rightChildNode);
			childs = Db.find(sql, levelIds, curLevelIds, ids);
		}
		if (childs.size() > 0) {
			for (Record child : childs) {
				OrganLevelRight olr = OrganLevelRight.dao.findByIds(child.getStr("rightIds"), levelIds);
				if (null != olr) {
					olr.delete();
				}
				if(stations.size() > 0){//批量删除岗位
					for(DeptStation station : stations){
						DeptStationRight oldStation = DeptStationRight.dao.findByIds(child.getStr("rightIds"), station.getPKValue());
						if(null != oldStation){
							oldStation.delete();
						}
					}
				}
				delChildLevelRight(child.getStr("rightIds"), levelIds, stations, curLevelIds);
			}
		}
	}

	// 递归删除父节点
	public void delParentLevelRight(String ids, String levelIds, List<DeptStation> stations) {
		if (null != ids) {
			Right right = Right.dao.findById(ids);
			if (null != right) {
				String sql = getSqlMy(OrganLevelRight.sqlId_countChild);
				List<OrganLevelRight> childs = OrganLevelRight.dao.find(sql, ids);
				if (null == childs || childs.size() == 0) {
					OrganLevelRight.dao.findByIds(ids, levelIds).delete();
					if(stations.size() > 0){//批量删除岗位
						for(DeptStation station : stations){
							DeptStationRight oldStation = DeptStationRight.dao.findByIds(ids, station.getPKValue());
							if(null != oldStation){
								oldStation.delete();
							}
						}
					}
					delParentLevelRight(right.getStr(Right.column_parentids), levelIds, stations);
				}
			}
		}
	}

}
